package org.senseframework.support.open.api.controller;


import org.senseframework.support.open.api.controller.encrypt.ApplicationEncryptHandler;
import org.senseframework.support.open.api.helper.ResponseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

/**
 * Created by lanjian
 */
@Controller
@RequestMapping("/")
public class ServiceController extends BasicController {
    private static Logger logger = LoggerFactory.getLogger(ServiceController.class);

    @Autowired
    private ApplicationEncryptHandler applicationEncryptHandler;

    public ApplicationEncryptHandler getApplicationEncryptHandler() {
        return applicationEncryptHandler;
    }

    public void setApplicationEncryptHandler(ApplicationEncryptHandler applicationEncryptHandler) {
        this.applicationEncryptHandler = applicationEncryptHandler;
    }

    /**
     * 进行密钥交互
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/exchange", method = RequestMethod.POST)
//    @RequestBody String body,
    public Map exchange(HttpServletRequest request) {
        try {
            //解密
            byte[] buffer = new byte[1024];
            int read = 0;
            ByteArrayOutputStream requestData = new ByteArrayOutputStream();
            InputStream inputStream = request.getInputStream();
            do {
                read = inputStream.read(buffer);
                if (read > 0) {
                    requestData.write(buffer, 0, read);
                }
            } while (read > 0);
            byte[] content = requestData.toByteArray();
            requestData.close();
            Object obj = applicationEncryptHandler.generate(content);
            return ResponseHelper.success(obj);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("exchange key error.", e);
            return ResponseHelper.fail();
        }
    }


    @ResponseBody
    @RequestMapping(value = "/{service}", method = {RequestMethod.POST})
    public Object service(@PathVariable String service,
                          @RequestHeader(value = "base-param", required = false) String baseParam,
                          @RequestHeader(value = "token", required = false) String token,
                          @RequestParam(value = "version") String version,
                          @RequestParam(value = "time") Long time,
                          @RequestParam(value = "data") String data,
                          @RequestParam(value = "salt") String salt,
                          @RequestParam(value = "sign") String sign,
                          @RequestParam(value = "appid") String appid,
                          HttpServletRequest request, HttpServletResponse response) {
        return handler(service, version, data, appid, salt, sign, time, request, response, null, applicationEncryptHandler.open() ? applicationEncryptHandler : null, baseParam, token);
    }

    @ResponseBody
    @RequestMapping(value = "/file/{service}", method = {RequestMethod.POST})
    public Object service(@PathVariable final String service,
                          @RequestHeader(value = "base-param", required = false) String baseParam,
                          @RequestHeader(value = "token", required = false) String token,
                          @RequestParam(value = "version") final String version,
                          @RequestParam(value = "time") final Long time,
                          @RequestParam(value = "salt") String salt,
                          @RequestParam(value = "data") final String data,
                          @RequestParam(value = "sign") final String sign,
                          @RequestParam(value = "appid") final String appid,
                          @RequestParam(value = "files") final MultipartFile[] files,
                          final HttpServletRequest request, final HttpServletResponse response) {
        return handler(service, version, data, appid, salt, sign, time, request, response, files, applicationEncryptHandler.open() ? applicationEncryptHandler : null, baseParam, token);
    }


}
